package com.example.chenjun.myapplication.activity;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.transition.Explode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.chenjun.myapplication.R;
import com.example.chenjun.myapplication.bean.DaoMaster;
import com.example.chenjun.myapplication.bean.DaoSession;
import com.example.chenjun.myapplication.bean.SearchHistoryBean;
import com.example.chenjun.myapplication.bean.SearchHistoryBeanDao;
import com.example.chenjun.myapplication.mvp.MvpActivity;
import com.example.chenjun.myapplication.widget.AutoNewLineLayout;

import java.util.List;

public class SearchActivity extends MvpActivity {

    private TextView mTvHistory;
    private AutoNewLineLayout mHistoryContainer;
    private EditText mEtSearch;
    private Button mBtnSearch;
    private SearchHistoryBeanDao mDao;
    private int state;
    private static final int NO_HISTORY = 0;
    private static final int HAS_HISTORY = 1;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        getWindow().setExitTransition(new Explode());
        setContentView(R.layout.app_activity_search);
        mTvHistory = findViewById(R.id.tv_no_history);
        mHistoryContainer = findViewById(R.id.container_history);
        mEtSearch = findViewById(R.id.et_search);
        mBtnSearch = findViewById(R.id.btn_search);
        mBtnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEtSearch.getText().length() == 0) {
                    return;
                }
                String searchKey = mEtSearch.getText().toString();
                startActivity(new Intent(SearchActivity.this, CookListActivity.class).putExtra("searchKey", searchKey));
                SearchHistoryBean entity = new SearchHistoryBean(searchKey);
                mDao.insertOrReplace(entity);
                mHistoryContainer.addView(createTipsTextView(entity));
                if (state == NO_HISTORY){
                    mHistoryContainer.setVisibility(View.VISIBLE);
                    mTvHistory.setVisibility(View.GONE);
                    state = HAS_HISTORY;
                }
            }
        });
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "cook");
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster master = new DaoMaster(db);
        DaoSession daoSession = master.newSession();
        mDao = daoSession.getSearchHistoryBeanDao();
        List<SearchHistoryBean> list = mDao.queryBuilder().list();
        if (list == null || list.isEmpty()) {
            mHistoryContainer.setVisibility(View.GONE);
            state = NO_HISTORY;
        } else {
            mTvHistory.setVisibility(View.GONE);
            state = HAS_HISTORY;
            for (SearchHistoryBean bean : list) {
                mHistoryContainer.addView(createTipsTextView(bean));
            }
        }
    }

    private TextView createTipsTextView(final SearchHistoryBean bean) {
        TextView tv = (TextView) LayoutInflater.from(this).inflate(R.layout.app_layout_tv_tips, mHistoryContainer, false);
        tv.setBackgroundResource(R.drawable.app_shape_tv_tips);
        tv.setText(bean.getSearchKey());
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SearchActivity.this, CookListActivity.class).putExtra("searchKey", bean.getSearchKey()));
            }
        });
        tv.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                mDao.delete(bean);
                mHistoryContainer.removeView(v);
                if (mHistoryContainer.getChildCount() == 0){
                    state = NO_HISTORY;
                    mHistoryContainer.setVisibility(View.GONE);
                    mTvHistory.setVisibility(View.VISIBLE);
                }
                return true;
            }
        });
        return tv;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        finishAfterTransition();
    }

    //    @Override
//    public void finish() {
//        super.finish();
//        finishAfterTransition();
//    }
}
